/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.SessionException;
import org.hibernate.SessionFactory;
import pojo.Nhanvien;
import util.HibernateUtil;

/**
 *
 * @author Quan
 */
public class DangNhapDAO {

    public static Nhanvien GetUserInfo(String tendangnhap, String matkhau) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String hql = "from Nhanvien where tendangnhap = :tendanhnhap and matkhau = :matkhau";
        Nhanvien nv;
        try {
            org.hibernate.Query query = session.createQuery(hql);
            query.setParameter("tendanhnhap", tendangnhap);
            query.setParameter("matkhau", matkhau);
            nv = (Nhanvien) query.uniqueResult();
        } catch (SessionException e) {
            throw new QueryException(hql);
        } finally {
            session.close();
        }
        return nv;
    }

    public static boolean CheckPassword(Nhanvien nv) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String hql = "from Nhanvien where tendangnhap = :tendanhnhap and matkhau = :matkhau";
        Nhanvien nv2;
        try {
            org.hibernate.Query query = session.createQuery(hql);
            query.setParameter("tendanhnhap", nv.getTendangnhap());
            query.setParameter("matkhau", nv.getMatkhau());
            nv2 = (Nhanvien) query.uniqueResult();
        } catch (SessionException e) {
            throw new QueryException(hql);
        } finally {
            session.close();
        }
        return (nv2 != null);
    }

    public static String LayLoaiToaiKhoan(Nhanvien nv) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String hql = "from Nhanvien where tendangnhap = :tendanhnhap and matkhau = :matkhau";
        Nhanvien nv2;
        try {
            org.hibernate.Query query = session.createQuery(hql);
            query.setParameter("tendanhnhap", nv.getTendangnhap());
            query.setParameter("matkhau", nv.getMatkhau());
            nv2 = (Nhanvien) query.uniqueResult();
        } catch (SessionException e) {
            throw new QueryException(hql);
        } finally {
            session.close();
        }
        if (nv2 == null) {
            return null;
        }
        return nv2.getLoaitk();
    }
}
